import IconFont from "@/component/Common/IconFont";
import xfZuoIcon from "@/assets/xfZuo.svg";

const SITES = Object.freeze({
  txXinWen: "txXinWen", // 腾讯新闻
  zh: "zh", //知乎
  dy: "dy", // 抖音
  xlWeiBo: "xlWeiBo", // 新浪微博
  ppXinWen: "ppXinWen", //澎湃新闻
  bdTieBa: "bdTieBa", // 百度贴吧
  ssPai: "ssPai", //少数派
  "36kr": "36kr", //36氪
  itZhiJia: "itZhiJia", //IT之家
  xfZuo: "xfZuo", //小饭桌
  fxBaoGao: "fxBaoGao", // 发现报告
  dcDi: "dcDi", //懂车帝
});

const SITES_URL = Object.freeze({
  fxBaoGao: "https://www.fxbaogao.com/", // 发现报告 日榜，周榜，月榜
  txXinWen: {
    hotSpot: "https://news.qq.com/",
    hotQuestion: "https://news.qq.com/ch/qa",
  }, // 腾讯新闻， 热搜，热问
  xlWeiBo:
    "https://m.weibo.cn/p/106003type=25&t=3&disable_hot=1&filter_type=realtimehot", // 新浪微博，热搜
  bdTieBa: "https://tieba.baidu.com/hottopic/browse/topicList?res_type=1", // 百度贴吧, 热议榜
  dy: "https://www.douyin.com/hot", // 抖音, 热榜
  ssPai: "https://sspai.com/", //少数派, 最新最热
  ppXinWen: "https://www.thepaper.cn/", //澎湃新闻,热榜
  zh: "https://www.zhihu.com/hot", //知乎,热榜
  dcDi: "https://www-hl.dongchedi.com/search?keyword=%E7%83%AD%E6%90%9C%E6%A6%9C&currTab=1&city_name=%E8%90%8D%E4%B9%A1&search_mode=common", //懂车帝， 热搜榜
  itZhiJia: "https://it.ithome.com/", //IT之家 最新，日榜，周榜，月榜
  xfZuo: "https://www.xfz.cn/", //小饭桌
  "36kr": {
    newsFlashes: "https://www.36kr.com/newsflashes/catalog/0",
    webNews: "https://www.36kr.com/information/web_news/latest/",
    hotRenQi: "https://www.36kr.com/hot-list/renqi/2025-04-29/1",
    hotZongHe: "https://www.36kr.com/hot-list/zonghe/2025-04-29/1",
    hotShouCang: "https://www.36kr.com/hot-list/shoucang/2025-04-29/1",
  }, //36氪
});

const SITES_INFO = Object.freeze({
  fxBaoGao: {
    iconName: "weibiaoti-1-05",
    name: "发现报告",
  },
  txXinWen: {
    iconName: "tengxunxinwen",
    name: "腾讯新闻",
  },
  zh: {
    iconName: "zhihu",
    name: "知乎",
  },
  xlWeiBo: {
    iconName: "xinlangweibo",
    name: "新浪微博",
  },
  dy: {
    iconName: "douyin",
    name: "抖音",
  },
  ppXinWen: {
    iconName: "a_pengpaixinwen",
    name: "澎湃新闻",
  },
  bdTieBa: {
    iconName: "baidutieba",
    name: "百度贴吧",
  },
  dcDi: {
    iconName: "dongchedi",
    name: "懂车帝",
  },
  xfZuo: {
    iconName: "xiaofanzhuo",
    name: "小饭桌",
  },
  "36kr": {
    iconName: "a-36kr",
    name: "36氪",
  },
  itZhiJia: {
    iconName: "itzhijia-copy-copy",
    name: "IT之家",
  },
  ssPai: {
    iconName: "sspai",
    name: "少数派",
  },
});

// 通常页数大小
const PAGE_SIZE = 10;

/**
 * 原地按order字段排序（升序）
 * @param {Array} data - 要排序的数组
 */
function sortByOrder(data) {
  if (!Array.isArray(data)) return [];

  // 使用slice()创建新数组避免严格模式警告
  return data.sort((a, b) => {
    const orderA = Number(a?.order) || 0;
    const orderB = Number(b?.order) || 0;
    return orderA - orderB;
  });
}

/**
 * 将前三名和置顶换成图标
 * @param {string} order -排名前三名换图标
 * @param {boolean} top 是否是置顶
 * @returns number or icon
 */
const getOrderIcon = (order, top) => {
  if (top) return <IconFont icon={"#icon-zhiding"} />;

  const ORDER = {
    1: <IconFont icon={"#icon-tongyong-di"} className={"icon-tongyong-di"} />,
    2: <IconFont icon={"#icon-tongyong-di1"} className={"icon-tongyong-di1"} />,
    3: <IconFont icon={"#icon-tongyong-di2"} className={"icon-tongyong-di2"} />,
  };
  return ORDER[order] ?? order;
};

/**
 * 安全的除法,保留指定小数
 * @param {number} a 被除数
 * @param {number} b  除数
 * @param {number} decimals 保留位数
 * @returns 运算后
 */
function safeDivide(a, b, decimals = 1) {
  if (b === 0) return NaN; // 或返回0/Infinity/其他默认值
  const factor = 10 ** decimals;
  return Math.round((a / b) * factor) / factor;
}

/**
 * 处理number数据转成万为单位
 * @param {string} number 处理的数据
 * @returns 处理后的string
 */
const formatNumberToWan = (number) => {
  const unit = 10 * 1000;
  let result = number;
  if (Number(number) > unit) {
    result = String(safeDivide(Number(number), unit)) + "万";
  }
  return result;
};

/**
 * 获取网站图标
 * @param {object} options 配置项
 * @param {string} options.site 网站名称
 * @param {number} options.width 图标宽度
 * @param {number} options.height 图标高度
 * @param {string} options.viewBox 图标视图框
 */
const getSiteIcon = ({ site, width, height, viewBox }) => {
  const siteInfo = SITES_INFO[site];
  if (!siteInfo) return null;
  const { iconName } = siteInfo;
  if (iconName === "xiaofanzhuo") {
    return (
      <img
        style={{ width, height, translate: "-1.5px 2px" }}
        src={xfZuoIcon}
        alt="小饭桌"
      />
    );
  }
  return (
    <IconFont
      icon={`#icon-${iconName}`}
      className={`icon-${iconName}`}
      width={width}
      height={height}
      viewBox={viewBox}
    />
  );
};

/**
 * 获取网站名称
 * @param {string} site 网站名称
 */
const getSiteName = (site) => {
  const siteInfo = SITES_INFO[site];
  if (!siteInfo) return null;
  return siteInfo.name;
};

/**
 * 获取所有网站key
 */
const getAllSitesKey = () => {
  return Object.values(SITES);
};

/**
 * 处理页数变换
 * @param {number} pageSize PAGE_SIZE倍数
 * @param {number} total 总数
 * @param {number} currentData 当前数据量
 */
const processCurrentPage = (pageSize, total, currentData) => {
  const currentLength = currentData?.length;
  // 正常pageSize
  if (pageSize === PAGE_SIZE) {
    return [currentLength / PAGE_SIZE + 1, PAGE_SIZE];
  }
  // 初始值
  if (currentLength === 0) return [1, pageSize];
  const currentPage = Math.floor(currentLength / pageSize) + 1;
  const currentPageSize = currentLength / (currentPage - 1);
  if (pageSize * currentPage >= total) {
    // 获得剩下所有数据，需要slice处理
    return [1, total];
  }
  return [currentPage, currentPageSize];
};

export {
  SITES,
  SITES_URL,
  PAGE_SIZE,
  sortByOrder,
  getOrderIcon,
  formatNumberToWan,
  getSiteIcon,
  getSiteName,
  getAllSitesKey,
  processCurrentPage,
};
